﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using points.Model;
using points.AppInfo;
using System.Web.Security;

namespace points
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Customer c = new Customer();

            c.Select(txtUsername.Text, txtPassword.Text);

            int pkCustTypeMerchant = AppVars.customer_type["merchant"].Pk;
            int pkCustTypeConsumer = AppVars.customer_type["consumer"].Pk;

            if (c.Pk > 0)
            {
                Session["CUSTOMER_PK"]=c.Pk;
                Session["CUSTOMER_TYPE_ID"] = c.Fk_Customer_Type_Id;

                if (c.Fk_Customer_Type_Id == pkCustTypeMerchant)
                {
                    //then it is a merchant account
                    //Response.Redirect("Merchant.aspx");
                    //FormsAuthentication.RedirectFromLoginPage("~/Merchant.aspx", true);
                    Response.Redirect("Merchant.aspx", true);
                }
                else if (c.Fk_Customer_Type_Id == pkCustTypeConsumer)
                {
                    //then it is a consumer account
                    //Response.Redirect("Consumer.aspx");
                    //FormsAuthentication.RedirectFromLoginPage("~/Consumer.aspx", true);
                    Response.Redirect("Consumer.aspx", true);
                }
                else
                {
                    //uh oh
                    lblMessage.Text = "Error";
                    lblResponse.Text = "Invalid User/Pass";
                }
            }
            else
            {
                //incorrect user pass
                lblMessage.Text = "Error";
                lblResponse.Text = "Invalid User/Pass";
            }
        }
    }
}